//
//  BusinessViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/28.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "BusinessViewController.h"

#import "TodoHttpManager.h"
#import "BusinessXVC.h"

@interface BusinessViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    NSMutableArray *_busArr;
}

@property(nonatomic,strong)UITableView *tableView;
@end

@implementation BusinessViewController


-(void)viewWillAppear:(BOOL)animated
{
    //商业房源通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDZhuSBusHouse:) name:TDZhuSBusHouse object:nil];


    
    //TODO:商业房源
  //  [_manager userZhuShouHouseBusness];
   
}

//商业房源通知方法
-(void)didTDZhuSBusHouse:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _busArr = [notifi.object objectForKey:@"data"];
         
           // NSLog(@"mmmmmmmm=========busArr %@",_busArr);
            [_tableView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"获取失败" andIsNeedTitle:NO andShowTime:2.0];
        }
    }
    
}
-(void)dealloc
{
    //移除商业房源通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDZhuSBusHouse object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
  
    _busArr = [NSMutableArray arrayWithCapacity:0];
    
    _manager = [TodoHttpManager sharedManager];
    //TODO:商业房源
    [_manager userZhuShouHouseBusness];
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 44;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    _tableView.backgroundColor = BgColor;
    [self.view addSubview:_tableView];
    
    //TODO:表头
    // [_manager userZhuShouHouseBiaoWithType:@"1" withQiShu:_houseID];
    
    __weak BusinessViewController *weakSelf = self;
    [_tableView addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
}

#pragma mark--刷新---------刷新--------
- (void)insertRowAtTop {
    __weak BusinessViewController *weakSelf = self;
    
    int64_t delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [weakSelf.tableView beginUpdates];
        
        
        [weakSelf.tableView endUpdates];
        
        [weakSelf.tableView.pullToRefreshView stopAnimating];
    });
}


- (void)insertRowAtBottom {
    __weak BusinessViewController *weakSelf = self;
    
    int64_t delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [weakSelf.tableView beginUpdates];
        
        [weakSelf.tableView endUpdates];
        
        [weakSelf.tableView.infiniteScrollingView stopAnimating];
    });
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _busArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifi = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifi];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifi];
        
    }
    NSDictionary *dict = [_busArr objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    cell.textLabel.textColor = OverColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消cell的点击效果
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    BusinessXVC *busVC = [[BusinessXVC alloc] init];
    busVC.IDStr = [[_busArr objectAtIndex:indexPath.row] objectForKey:@"name"];
    busVC.zongStr =[[_busArr objectAtIndex:indexPath.row] objectForKey:@"all_rooms"];
    busVC.yiStr = [[_busArr objectAtIndex:indexPath.row] objectForKey:@"sale_rooms"];
    busVC.weiStr = [[_busArr objectAtIndex:indexPath.row] objectForKey:@"rest_rooms"];
  //NSLog(@"nnnnnnn====%@____%@------%@=====IDStr%@",busVC.IDStr,busVC.zongStr,busVC.yiStr,busVC.weiStr );
    [self.navigationController pushViewController:busVC animated:NO];
    //BusRenXViewController *
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
